package com.webapp.common;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * 跨域配置
 * @author user
 */
@Configuration
public class CorsConfig {
	private CorsConfiguration buildConfig() {
		CorsConfiguration config = new CorsConfiguration();
		config.addAllowedOrigin("*");// 允许域名
		config.addAllowedHeader("*");// 允许header设置
		config.addAllowedMethod("*");// 任何方法,post get 等
		// allowCredential 需设置为true
		config.setAllowCredentials(true);
		return config;
	}

	@Bean
	public CorsFilter corsFilter() {
		UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
		source.registerCorsConfiguration("/**", buildConfig());
		return new CorsFilter(source);
	}
}